/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils.easing
{
	public class BothEndsEasing extends Easing implements IEasing
	{
		private var _x0:Number;
		private var _length:Number;
		private var _order:Number;
		private var _half:Number;
		/**
		 * Both Ends Easing
		 * @param start Start number
		 * @param end End number
		 * @param count The number of frames
		 * @param order Easing order
		 * 
		 */
		public function BothEndsEasing(start:Number, end:Number, count:uint, order:uint)
		{
			updateParams(start, end, count, order);
		}
		
		public function updateParams(start:Number, end:Number, count:uint, order:uint):void {
			if (order < 1) {
				throw new ArgumentError("order should be a natural number");
			}
			frame = count;
			_x0 = start;
			_length = end - start;
			_order = order;
			_half = 2*f(1/2);
			initialize();
		}

		public function forward():Number
		{
			var pp:Number;
			if (isMoving()) {
				pos++;
				if (pos < frame / 2) {
					pp = _x0 + _length / _half * f(pos/frame);
				} else {
					pp = _x0 + _length - _length / _half * f(1-pos/frame);
				}
			} else {
				pp = _x0 + _length;
			}
			return pp;
		}
		
		private function f(x:Number):Number {
			var y:Number = 1;
			for(var i:int = 0;i < _order;i++) {
				y *= x;
			}
			return y;
		}
		
	}
}